#include <stdio.h>
#include <iostream>
#include <mmintrin.h>   //mmx
#include <xmmintrin.h>  //sse
#include <emmintrin.h>  //sse2
#include <pmmintrin.h>  //sse3

#define MAX 10000
int main(int argv,char *args[])
{
    clock_t start,end;
    float a[MAX][4];	
    float b[MAX][4];	
    float *pa[MAX][4];
    const float *pb[MAX][4];
    int i;
    for(i=0;i<MAX;i++)
    {
        a[i][0]=i;
        a[i][1]=i;
        a[i][2]=i;
        a[i][3]=i;
        pa[i][0]=&(a[i][0]);
        pa[i][1]=&(a[i][1]);
        pa[i][2]=&(a[i][2]);
        pa[i][3]=&(a[i][3]);
        b[i][0]=-2;
        b[i][1]=-3;
        b[i][2]=-4;
        b[i][3]=-5;
        pb[i][0]=&(b[i][0]);
        pb[i][1]=&(b[i][1]);
        pb[i][2]=&(b[i][2]);
        pb[i][3]=&(b[i][3]);
    }
    std::cout<<"矩阵初始化完成"<<std::endl;

    start = clock();
    for( i=0;i<MAX;i++)
    {
        a[i][0] = *pa[i][0]*(*pb[i][0]);
        a[i][1] = *pa[i][1]*(*pb[i][1]);
        a[i][2] = *pa[i][2]*(*pb[i][2]);
        a[i][3] = *pa[i][3]*(*pb[i][3]);
        //b[i][0] = *pb[i][0];
    }
    end = clock();
    double time_cost = (double)(end-start);
    std::cout<< "直接用乘法耗时:"<<time_cost<<"ms"<<std::endl;
    /*
    std::cout<<"pa[0]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][0])<<",";}
    std::cout<<std::endl;

    std::cout<<"pa[1]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][1])<<",";}
    std::cout<<std::endl;

    std::cout<<"pa[2]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][2])<<",";}
    std::cout<<std::endl;

    std::cout<<"pa[3]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][3])<<",";}
    std::cout<<std::endl;
     */
    start = clock();
    for( i=0;i<MAX;i++)
    {
        __m128 sse_1 = _mm_load_ps(pa[i][0]);
        __m128 sse_2 = _mm_load_ps(pb[i][0]);
        __m128 h = _mm_mul_ps(sse_1,sse_2);
        _mm_store_ps(pa[i][0],h);
    }
    end = clock();
    double time_cost_sse=(double)(end-start);
    std::cout<< "sse指令计算耗时:"<< time_cost_sse<<"ms"<<std::endl;
    std::cout<< "耗时降低(性能提升)："<< (time_cost-time_cost_sse)/time_cost<<std::endl;
    /*
    std::cout<<"pa[0]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][0])<<",";}
    std::cout<<std::endl;

    std::cout<<"pa[1]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][1])<<",";}
    std::cout<<std::endl;

    std::cout<<"pa[2]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][2])<<",";}
    std::cout<<std::endl;

    std::cout<<"pa[3]:"<<std::endl;
    for(i=0;i<MAX/100;i++){std::cout<<*(pa[i][3])<<",";}
    std::cout<<std::endl;
    */

       
}
